package org.dousb.douojbackendjudgeservice.judge.codesandbox.impl;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.dousb.douojbackend.common.common.ErrorCode;
import org.dousb.douojbackend.common.exception.BusinessException;
import org.dousb.douojbackend.model.codesandbox.ExecuteCodeRequest;
import org.dousb.douojbackend.model.codesandbox.ExecuteCodeResponse;
import org.dousb.douojbackendjudgeservice.judge.codesandbox.CodeSandbox;

/**
 * RemoteCodeSandbox
 * 远程代码沙箱
 *
 * @author 豆沙包
 * @version 1.0
 * @since 2025-11-01  15:11
 */


public class RemoteCodeSandbox implements CodeSandbox {


    private static final String AUTH_HEADER = "auth";

    private static final String AUTH_REQUEST_SECRET = "secret";

    /**
     * 执行代码
     *
     * @param request 请求参数
     * @return 执行结果
     */
    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest request) {
        System.out.println("远程代码沙箱");
        String url = "http://192.168.75.33:8080/executeCode";
        String json = JSONUtil.toJsonStr(request);
        String responseStr = HttpUtil.createPost(url)
                .body(json)
                .header(AUTH_HEADER, AUTH_REQUEST_SECRET)
                .execute()
                .body();
        if (StringUtils.isBlank(responseStr)) {
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR, "exectueCode remoteSandbox error,message = " + responseStr);
        }
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}